package com.supply.dl.app.http;


import com.supply.dl.app.AppConfig;
import com.supply.dl.global.Global;
import com.supply.dl.utils.StringUtils;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 获取Retrofit
 *
 * @author skyTian
 * @desc 获取Retrofit对象的基类
 * @date 2017/9/7 15:49
 */
public class HttpMethod {

    private static volatile OkHttpClient client, hasTokenClient;
    private static volatile Retrofit builder;

    /**
     * double-check
     *
     * @return
     */
    public static Retrofit getInstance() {
        final String token = Global.getInstance().getToken();
        String httpString=Global.getInstance().getHttpAddress();
        String baseUrl="";
        if (!StringUtils.isEmpty(httpString)){
            baseUrl=httpString;
        }else {
            baseUrl=AppConfig.BASE_URL;
        }
        //没有token的时候
        if (!StringUtils.isCanUse(token)) {
            if (client == null) {
                synchronized (HttpMethod.class) {
                    if (client == null) {
                        //log日志
                        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

                        client = new OkHttpClient.Builder()
                                .addInterceptor(new Interceptor() {
                                    @Override
                                    public Response intercept(Chain chain) throws IOException {
                                        Request request = chain.request()
                                                .newBuilder()
                                                .addHeader("accept", "application/json")
                                                .addHeader("userId", "")
                                                .build();
                                        return chain.proceed(request);
                                    }
                                })
                                .addInterceptor(loggingInterceptor)
                                .readTimeout(5, TimeUnit.MINUTES)
                                .writeTimeout(5, TimeUnit.MINUTES)
                                .connectTimeout(20, TimeUnit.SECONDS)
                                .build();
                    }
                }
            }
            builder = new Retrofit.Builder()
//                    .baseUrl(AppConfig.BASE_URL)
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(client)
                    .build();
        }
        // 有token的时候
        else {
            if (hasTokenClient == null) {
                synchronized (HttpMethod.class) {
                    if (hasTokenClient == null) {
                        //log日志
                        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

                        hasTokenClient = new OkHttpClient.Builder()
                                .addInterceptor(new Interceptor() {
                                    @Override
                                    public Response intercept(Chain chain) throws IOException {
                                        Request request = chain.request()
                                                .newBuilder()
                                                .addHeader("accept", "application/json")
                                                .addHeader("userId", token)
                                                .build();
                                        return chain.proceed(request);
                                    }
                                })
                                .addInterceptor(loggingInterceptor)
                                .readTimeout(5, TimeUnit.MINUTES)
                                .writeTimeout(5, TimeUnit.MINUTES)
                                .connectTimeout(20, TimeUnit.SECONDS)
                                .build();
                    }
                }
            }

            builder = new Retrofit.Builder()
//                    .baseUrl(AppConfig.BASE_URL)
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(hasTokenClient)
                    .build();
        }
        return builder;
    }


}
